package cn.geminis.workflow.service.service.engine;

import org.flowable.bpmn.model.ExtensionElement;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public class ExtensionElementUtils {

    public static Optional<String> get(Map<String, List<ExtensionElement>> extensions, String name) {
        return Optional.ofNullable(extensions.get("extension"))
                .map(elements -> elements.stream()
                        .filter(extensionElement -> extensionElement.getAttributes().containsKey(name))
                        .findAny()
                        .map(extensionElement -> {
                            var value = extensionElement.getAttributeValue(null, name);
                            return StringUtils.hasText(value) ? value : null;
                        })
                        .orElse(null));
    }

}
